<template>
    <view class="chat-container">
        <!-- 机器人介绍 -->
        <view class="robot-intro">
            <image class="robot-avatar"
                src="https://heda123.oss-cn-guangzhou.aliyuncs.com/upload/3/20250121/e41d7fe8ead48f06290c699d6d6d36e9.jpg"
                mode="aspectFill"></image>
            <view class="intro-content">
                <view class="intro-title">
                    <text class="title-text">Hi, 远方好物会员</text>
                    <text class="protocol">协议</text>
                </view>
                <view class="intro-desc">我是您的专属健康营养师 远宝</view>
                <view class="intro-desc">可为您提供营养知识咨询，还能找商品和素材，为您的私域运营推广助力！</view>
            </view>
        </view>

        <!-- 快捷问题列表 -->
        <view class="quick-questions">
            <view class="question-item" v-for="(question, index) in quickQuestions" :key="index"
                @click="askQuestion(question)">
                <text class="question-text">{{ question }}</text>
                <image class="question-icon" src="../static/img/album.png" mode="aspectFit"></image>
            </view>
        </view>

        <!-- 聊天内容区域  -->
        <scroll-view class="chat-content" scroll-y="true" :scroll-top="scrollTop" :scroll-with-animation="true"
            :scroll-into-view="lastMessageId" @scrolltoupper="loadMoreMessages">
            <view class="message-list">
                <view v-for="(msg, index) in messages" :key="index" :id="'msg-' + index"
                    :class="['message-item', msg.type === 'user' ? 'user-message' : 'ai-message']">
                    <image class="avatar"
                        :src="msg.type === 'user' ? '../static/img/album.png' : 'https://heda123.oss-cn-guangzhou.aliyuncs.com/upload/3/20250121/e41d7fe8ead48f06290c699d6d6d36e9.jpg'"
                        mode="aspectFill"></image>
                    <view class="message-content">{{ msg.content }}</view>
                </view>
            </view>
        </scroll-view>

        <!-- 底部输入区域 -->
        <view class="input-area">
            <view class="input-box">
                <input type="text" v-model="inputMessage" placeholder="请输入您想咨询的问题" @confirm="sendMessage"
                    confirm-type="send" />
                <view class="analysis-btn" @click="showAnalysis">
                    <image src="../static/img/album.png" mode="aspectFit"></image>
                    <text>报告分析</text>
                </view>
            </view>
            <view class="send-btn" @click="sendMessage">
                <image src="../static/img/album.png" mode="aspectFit"></image>
            </view>
        </view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            quickQuestions: [
                '有什么适合给孩子当早餐的？',
                '给我一份关于远方桦树汁的介绍？',
                'SGS是什么，能检测哪些的营养成分？'
            ],
            messages: [],
            inputMessage: '',
            scrollTop: 0,
            lastMessageId: ''
        }
    },
    methods: {
        // 发送快捷问题
        askQuestion(question) {
            this.addMessage('user', question)
            // 模拟AI回复
            setTimeout(() => {
                this.addMessage('ai', this.getAIResponse(question))
            }, 500)
        },
        // 发送消息
        sendMessage() {
            if (!this.inputMessage.trim()) return
            this.addMessage('user', this.inputMessage)
            this.inputMessage = ''
            // 模拟AI回复
            setTimeout(() => {
                this.addMessage('ai', '感谢您的提问！我正在思考中...')
            }, 500)
        },
        // 添加消息到列表
        addMessage(type, content) {
            const messageIndex = this.messages.length
            this.messages.push({
                type,
                content,
                timestamp: new Date().getTime()
            })
            this.$nextTick(() => {
                this.lastMessageId = 'msg-' + messageIndex
                setTimeout(() => {
                    this.scrollToBottom()
                }, 100)
            })
        },
        // 滚动到底部
        scrollToBottom() {
            const query = uni.createSelectorQuery().in(this)
            query.select('.chat-content').boundingClientRect(data => {
                if (data) {
                    this.scrollTop = data.height * 2
                }
            }).exec()
        },
        // 加载更多消息
        loadMoreMessages() {
            // 实现加载历史消息的逻辑
        },
        // 显示报告分析
        showAnalysis() {
            uni.showToast({
                title: '报告分析功能开发中',
                icon: 'none'
            })
        },
        // 模拟AI回复
        getAIResponse(question) {
            const responses = {
                '有什么适合给孩子当早餐的？': '建议可以给孩子准备以下早餐：\n1. 全麦面包配牛奶\n2. 燕麦粥搭配水果\n3. 鸡蛋三明治\n4. 玉米粥配小菜\n这些都是营养均衡、容易消化的选择。',
                '给我一份关于远方桦树汁的介绍？': '远方桦树汁是一种天然的植物饮品，具有以下特点：\n1. 富含矿物质和维生素\n2. 零添加、零防腐\n3. 具有清热解火的功效\n4. 适合各年龄段人群饮用',
                'SGS是什么，能检测哪些的营养成分？': 'SGS是全球领先的检验、验证、测试和认证机构。可以检测：\n1. 蛋白质含量\n2. 维生素含量\n3. 矿物质含量\n4. 重金属含量\n5. 农药残留等'
            }
            return responses[question] || '抱歉，我需要更多信息来回答这个问题。'
        }
    }
}
</script>

<style lang="scss">
.chat-container {
    height: 100vh;
    display: flex;
    flex-direction: column;
    background: #f5f5f5;

    .robot-intro {
        padding: 30rpx;
        background: #fff;
        display: flex;
        align-items: flex-start;

        .robot-avatar {
            width: 120rpx;
            height: 120rpx;
            border-radius: 60rpx;
            margin-right: 20rpx;
        }

        .intro-content {
            flex: 1;

            .intro-title {
                display: flex;
                justify-content: space-between;
                align-items: center;
                margin-bottom: 10rpx;

                .title-text {
                    font-size: 32rpx;
                    color: #333;
                    font-weight: bold;
                }

                .protocol {
                    font-size: 24rpx;
                    color: #999;
                }
            }

            .intro-desc {
                font-size: 26rpx;
                color: #666;
                line-height: 1.6;
            }
        }
    }

    .quick-questions {
        padding: 20rpx;
        background: #fff;

        .question-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 24rpx;
            background: #f8f8f8;
            border-radius: 12rpx;
            margin-bottom: 20rpx;

            .question-text {
                font-size: 28rpx;
                color: #333;
            }

            .question-icon {
                width: 40rpx;
                height: 40rpx;
            }
        }
    }

    .chat-content {
        flex: 1;
        padding: 20rpx;
        overflow-y: auto;
        height: 0;

        .message-list {
            .message-item {
                display: flex;
                margin-bottom: 30rpx;

                .avatar {
                    width: 80rpx;
                    height: 80rpx;
                    border-radius: 40rpx;
                    margin: 0 20rpx;
                }

                .message-content {
                    max-width: 70%;
                    padding: 20rpx;
                    border-radius: 12rpx;
                    font-size: 28rpx;
                    line-height: 1.6;
                }

                &.user-message {
                    flex-direction: row-reverse;

                    .message-content {
                        background: #00b578;
                        color: #fff;
                        margin-right: 20rpx;
                    }
                }

                &.ai-message {
                    .message-content {
                        background: #fff;
                        color: #333;
                        margin-left: 20rpx;
                    }
                }
            }
        }
    }

    .input-area {
        padding: 20rpx;
        background: #fff;
        display: flex;
        align-items: center;
        border-top: 1px solid #eee;

        .input-box {
            flex: 1;
            display: flex;
            align-items: center;
            background: #f5f5f5;
            border-radius: 36rpx;
            padding: 0 20rpx;
            margin-right: 20rpx;

            input {
                flex: 1;
                height: 72rpx;
                font-size: 28rpx;
            }

            .analysis-btn {
                display: flex;
                align-items: center;
                padding: 0 20rpx;
                border-left: 1px solid #ddd;

                image {
                    width: 40rpx;
                    height: 40rpx;
                    margin-right: 10rpx;
                }

                text {
                    font-size: 26rpx;
                    color: #666;
                }
            }
        }

        .send-btn {
            width: 72rpx;
            height: 72rpx;
            background: #00b578;
            border-radius: 36rpx;
            display: flex;
            align-items: center;
            justify-content: center;

            image {
                width: 40rpx;
                height: 40rpx;
            }
        }
    }
}
</style>
